/*
 * Copyright 2010 Pierre Heinzerling, Uwe Janner, Patrick Wright
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.upp.publinksky.dao;

import com.google.appengine.api.datastore.Key;
import com.upp.publinksky.model.Link;
import com.upp.publinksky.model.LinkComment;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

/**
 */
@SuppressWarnings({"unchecked"})
public class LinkCommentDAO {
    private static final Logger log = Logger.getLogger(LinkCommentDAO.class.getName());
    public static LinkComment getLinkCommentByUriAndEmail(final String email, final String uriString, final PersistenceManager pm) {
        Query qry = pm.newQuery(LinkComment.class);
        qry.setFilter("uriString == nu && userEmail == em");
        qry.declareParameters("String nu, String em");
        List<LinkComment> res = (List<LinkComment>) qry.executeWithArray(uriString, email);
        return res.size() == 0 ? null : res.get(0);
    }

    public static List<LinkComment> listCommentsForThemeAndUri(final Key theme, final String uri, final PersistenceManager pm) {
        Query query = pm.newQuery(LinkComment.class);
        query.setFilter("theme == thm && uriString == uri");
        query.declareParameters("com.google.appengine.api.datastore.Key thm, String uri");
        log.info(String.format("search for comments for theme %d url %s", theme.getId(), uri));
        return (List<LinkComment>) query.executeWithArray(theme, uri);
    }

    public static List<LinkComment> listCommentsForLink(final Link link, final PersistenceManager pm) {
        Key tkey = link.getTheme();
        if (tkey == null) {
            throw new RuntimeException("link theme key is null!");
        } else {
            return listCommentsForThemeAndUri(link.getTheme(), link.getUriString(), pm);
        }
    }

    public static LinkComment insertComment(final Key themeKey, final String email, final String uriString, final String note, final int lastUpdateCounter, final PersistenceManager pm) {
        LinkComment lc = new LinkComment();
        lc.setTheme(themeKey);
        lc.setUriString(uriString);
        lc.setUserEmail(email);
        lc.setNote(note);
        lc.setGood(true);
        lc.setLastUpdated(new Date());
        lc.setThemeLastUpdateCounter(lastUpdateCounter);
        pm.makePersistent(lc);
        return lc;
    }

    public static void updateLinkComment(final LinkComment lc, final String newNote, final int lastUpdateCounter, final PersistenceManager pm) {
        lc.setNote(newNote);
        lc.setLastUpdated(new Date());
        lc.setGood(true);
        lc.setThemeLastUpdateCounter(lastUpdateCounter);
        pm.makePersistent(lc);
    }

    public static LinkComment markNotGood(final LinkComment comment, final int lastUpdateCounter, final PersistenceManager pm) {
        comment.setGood(false);
        comment.setLastUpdated(new Date());
        comment.setThemeLastUpdateCounter(lastUpdateCounter);
        pm.makePersistent(comment);
        return comment;
    }
}
